﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for TypeKPIHandler
    /// </summary>
    public class TypeKPIHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            string action = HttpContext.Current.Request.QueryString["action"];
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["ID"]);
            string Description = HttpContext.Current.Request.QueryString["Description"];
            int GroupKPIID = HttpContext.Current.Request.QueryString["GroupKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["GroupKPI"]);
            int CategoryKPIID = HttpContext.Current.Request.QueryString["CategoryKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["CategoryKPI"]);
            bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            int Sequence = Convert.ToInt16(HttpContext.Current.Request.QueryString["Sequence"]);

            TypeKPIFacade ef = new TypeKPIFacade();
            TypeKPI Types = ef.GetTypeKPIByID(ID);
            TypeKPIValidator ev = new TypeKPIValidator();

            if (Types == null)
            {
                Types = new TypeKPI();
                Types.ID = ID;
                Types.Description = Description;
                Types.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(Convert.ToInt16(GroupKPIID));
                Types.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(Convert.ToInt16(CategoryKPIID));
                Types.isActive = isActive;
                Types.CreateBy = CurrUser.Email;
                Types.Sequence = Sequence;
                msg = ev.ValidateOnSave(Types);
            }
            else if (action != "delete")
            {
                Types.Description = Description;
                Types.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(Convert.ToInt16(GroupKPIID));
                Types.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(Convert.ToInt16(CategoryKPIID));
                Types.isActive = isActive;
                Types.LastModifiedBy = CurrUser.Email;
                Types.Sequence = Sequence;
                msg = ev.ValidateOnUpdate(Types);
            }
            else if(action == "delete")
                Types.LastModifiedBy = CurrUser.Email;

            if (msg == "OK")
            {
                using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    switch (action)
                    {
                        case "save":
                            msg = ef.Save(Types);
                            break;
                        case "delete":
                            msg = ef.BeforeDelete(Types);
                            if (msg == "0")
                                msg = ef.Delete(Types);
                            else
                                msg = "Data tidak bisa di hapus,dipakai pada tabel yang lain !";
                            break;
                    }
                    trans.Complete();
                    trans.Dispose();
                }
            }

            return msg;
        }
    }
}